package control{
	
	import event.CustomEvent;
	
	import flash.events.EventDispatcher;
	
	import vo.CardVo;
	import vo.GameModel;
	
	public class GameRule extends EventDispatcher{
		
		private var gameModel:GameModel = GameModel.getInstance();
//		private var gameModel2:GameModel = new GameModel();
		
		public function GameRule(){
			
		}
		
		public function startGame():void{
			gameModel.initData();
			gameModel.gameCardsVO.myCardList.push(new CardVo(gameModel.cardMachine.hitCard()));
			dispatchEvent(new CustomEvent(CustomEvent.GAME_DISPLAY));
		}
		
		public function gameGetCard():void{
			if(getTotalValue(gameModel.gameCardsVO.myCardList) < 21){
				gameModel.gameCardsVO.myCardList.push(new CardVo(gameModel.cardMachine.hitCard()));
				dispatchEvent(new CustomEvent(CustomEvent.GAME_DISPLAY2));
			}else{
				
			}
		}
		
		public function gameEnd():void{
			trace("gameEnd");
		}
		
		public function gameEnd2():void{
			
		}
		
		//计算所有牌总值
		private function getTotalValue(ary:Array):int{
			var sum:int = 0;
			for(var i:uint=0; i<ary.length; i++){
				var cardVo:CardVo = ary[i] as CardVo;
				var cardVal:int = cardVo.value;
				if(cardVal >=10){
					sum += 10;
				}else{
					sum += cardVal;
				}
			}
			return sum;
		}
	}
}